package com.his.login.service.Imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.his.login.dao.DictDoctorMapper;
import com.his.login.dao.DictEmployeeMapper;
import com.his.login.entity.DictDoctor;
import com.his.login.entity.DictEmployee;
import com.his.login.service.EmployeeService;
import com.his.login.vo.DictDoctorVo;
import com.his.login.vo.EmpInfoVo;
import com.his.login.vo.EmployeeVo;
import com.his.login.vo.PageEmployeeVo;
import com.his.menu.service.MenuService;
import com.xiao.common.exception.HisExecption;
import com.xiao.common.util.BeanUtil;
import com.xiao.common.util.MD5Util;
import com.xiao.common.util.PinYin4jUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author xiaoxiaoping
 * @Date 2020/2/3 16:09
 * @Version 1.0
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Resource
    private DictEmployeeMapper dictEmployeeMapper;
    @Resource
    private DictDoctorMapper dictDoctorMapper;
    @Resource
    private MenuService menuService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveEmployee(DictEmployee dictEmployee,EmployeeVo employeeVo){
        //判断用户是否存在
        String hospId;
        DictEmployee dictEmployee1 = new DictEmployee();
        if(StringUtils.isEmpty(employeeVo.getHOSPID())){
            hospId = dictEmployee.getHOSPID();
            dictEmployee1.setEMPID(hospId.concat(employeeVo.getEMPID()));
        }else{
            hospId = employeeVo.getHOSPID();
            dictEmployee1.setEMPID(employeeVo.getEMPID());
        }
        dictEmployee1.setHOSPID(hospId);
        List<DictEmployee> list = dictEmployeeMapper.selectByEntity(dictEmployee1);
        if(list.size()> 0){
            throw new HisExecption("雇员编码已存在，请新输入！");
        }
        DictEmployee dictEmployeeNew = BeanUtil.copyBean(employeeVo, DictEmployee.class);
        if(StringUtils.isEmpty(employeeVo.getHOSPID())){
            dictEmployeeNew.setEMPID(dictEmployee.getHOSPID().concat(employeeVo.getEMPID()));
        }else{
            //新增医院用户id直接就是
            dictEmployeeNew.setEMPID(employeeVo.getEMPID());
        }
        dictEmployeeNew.setINPUTSTR(PinYin4jUtil.converterToFirstSpell(employeeVo.getEMPNAME()));
        if(null == dictEmployeeNew.getSTOPFLAG()) {
            dictEmployeeNew.setSTOPFLAG(0);
        }
        dictEmployeeNew.setHOSPID(hospId);
        if(StringUtils.isEmpty(employeeVo.getEMPPASSWORD())) {
            dictEmployeeNew.setEMPPASSWORD(MD5Util.MD5Encode("e10adc3949ba59abbe56e057f20f883e","utf-8"));
        }else{
            dictEmployeeNew.setEMPPASSWORD(MD5Util.MD5Encode(employeeVo.getEMPPASSWORD(), "utf-8"));
        }
        dictEmployeeMapper.insertAllColumn(dictEmployeeNew);
    }

    /**
     * 修改密码
     * @param employeeVo
     */
    @Override
    public void updateEmployee(EmployeeVo employeeVo) {
        DictEmployee dictEmployee = BeanUtil.copyBean(employeeVo, DictEmployee.class);
        if(!StringUtils.isEmpty(dictEmployee.getEMPPASSWORD())){
            dictEmployee.setEMPPASSWORD(MD5Util.MD5Encode(dictEmployee.getEMPPASSWORD(), "utf-8"));
        }
        dictEmployeeMapper.updateById(dictEmployee);
    }

    @Override
    public List<DictEmployee> queryEmployeeByEntity(EmployeeVo employeeVo) {
        DictEmployee dictEmployee = BeanUtil.copyBean(employeeVo, DictEmployee.class);
        return dictEmployeeMapper.selectByEntity(dictEmployee);
    }

    /**
     * 雇员分页列表
     * @param dictEmployee
     * @param pageEmployeeVo
     * @return
     */
    @Override
    public PageInfo pageSelectEmployee(DictEmployee dictEmployee,PageEmployeeVo  pageEmployeeVo) {
        //第一个参数为页数，第二个为显示数
        PageHelper.startPage(pageEmployeeVo.getPage(),pageEmployeeVo.getLimit());
        //去数据库中查询数据
        Map params = new HashMap();
        params.put("hospId",dictEmployee.getHOSPID());
        params.put("orgId",pageEmployeeVo.getOrgId());
        params.put("empName",pageEmployeeVo.getEmpName());
        List<DictEmployee> dictEmployeeList = dictEmployeeMapper.selectByMap(params);
        //获取分页后的数据和信息
        PageInfo pageInfo = new PageInfo(dictEmployeeList);
        return pageInfo;
    }

    @Override
    public DictEmployee selectByEmpid(String empid) {
        return dictEmployeeMapper.selectByEmpId(empid);
    }

    @Override
    public DictDoctor selectByPrimaryKey(String empId) {
        return dictDoctorMapper.selectByPrimaryKey(empId);
    }

    @Override
    public void insertDictDoctor(DictDoctorVo dictDoctorVo) {
        DictDoctor dictDoctor = BeanUtil.copyBean(dictDoctorVo,DictDoctor.class);
        DictDoctor dictDoctor1 = dictDoctorMapper.selectByPrimaryKey(dictDoctor.getEmpid());
        if(null == dictDoctor1) {
            dictDoctorMapper.insert(dictDoctor);
        }else{
            dictDoctorMapper.updateByPrimaryKey(dictDoctor);
        }
    }

    /**
     *获取登录用户的信息
     * @param dictEmployee
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public EmpInfoVo getEmpInfo(DictEmployee dictEmployee) {
        EmpInfoVo empInfoVo = new EmpInfoVo();
        empInfoVo.setEmpid(dictEmployee.getEMPID());
        empInfoVo.setEmpname(dictEmployee.getEMPNAME());
        List<String> menuList = menuService.getMenuByEmp(dictEmployee.getEMPID());
        empInfoVo.setMenuList(menuList);
        return empInfoVo;
    }
}
